package com.carey.blog.framework.latke.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

/**
 * String utilities.
 * 
 */
public final class Strings {

	/**
	 * Private default constructor.
	 */
	private Strings() {
	}

	/**
	 * Converts the specified string into a string list line by line.
	 * 
	 * @param string
	 *            the specified string
	 * @return a list of string lines
	 * @throws IOException
	 *             io exception
	 */
	public static List<String> toLines(final String string) throws IOException {
		final BufferedReader bufferedReader = new BufferedReader(
				new StringReader(string));
		final List<String> ret = new ArrayList<String>();

		try {
			String line = bufferedReader.readLine();
			while (null != line) {
				ret.add(line);
				line = bufferedReader.readLine();
			}
		} finally {
			bufferedReader.close();
		}

		return ret;
	}

	/**
	 * Determines whether the specified string is {@code ""} or {@code null}.
	 * 
	 * @param string
	 *            the specified string
	 * @return {@code true} if the specified string is {@code ""} or
	 *         {@code null}, {@code false} otherwise
	 */
	public static boolean isEmptyOrNull(final String string) {
		return (string == null || string.trim().length() == 0);
	}

	/**
	 * Trims every string in the specified strings array.
	 * 
	 * @param strings
	 *            the specified strings array
	 * @return a trimmed strings array
	 */
	public static String[] trimAll(final String[] strings) {
		final String[] ret = new String[strings.length];

		for (int i = 0; i < strings.length; i++) {
			ret[i] = strings[i].trim();
		}

		return ret;
	}
}
